package com.hxminer.hxwebs.controller;

import com.hxminer.hxwebs.enmus.ResultEnum;
import com.hxminer.hxwebs.exceptions.HxException;
import com.hxminer.hxwebs.model.*;
import com.hxminer.hxwebs.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Author YRG
 * Created by on 2018/5/16.
 * Describe
 */
@RestController
public class OrdersController {

    @Autowired
    OrdersService ordersService;

    /**
     * 订单提交状态
     *
     * @param reOrders
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/hxminer/commit/orders")
    public Result<ReOrderNo> commitOrdersDetail(@Valid ReOrders reOrders) throws Exception {
        if (reOrders == null || "".equals(reOrders.getUsername())) throw new HxException(ResultEnum.UNKONW_ERROR);
        return ordersService.commitOrders(reOrders);
    }

    /**
     * 返回订单详情
     *
     * @param reOrders
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/hxminer/give/ordersDetails")
    public Result<ReOrders> findByOrders(@Valid ReOrders reOrders) throws Exception {
        if (reOrders == null || "".equals(reOrders.getOrdersNo()) || null == reOrders.getOrdersNo())
            throw new HxException(ResultEnum.UNKONW_ERROR);
        return ordersService.findByUserOrders(reOrders);
    }

    /**
     * 结算提交完成
     *
     * @param reOrders
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/hxminer/complete/orders")
    public Result<ReOrderNo> completeOrder(@Valid ReOrders reOrders) throws Exception {

        if (null == reOrders || "".equals(reOrders.getUserId()) || "".equals(reOrders.getOrdersNo())) {
            throw new HxException(ResultEnum.UNKONW_ERROR);
        }
        return ordersService.completeOrders(reOrders);
    }

    /**
     * 完成订单获取数据
     *
     * @param reOrders
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/hxminer/give/complete/ordersDetails")
    public Result<ReOrders> givePayOrder(@Valid ReOrders reOrders) throws Exception {

        if (null == reOrders || "".equals(reOrders.getUserId()) || "".equals(reOrders.getOrdersNo()) || null == reOrders.getOrdersNo() || null == reOrders.getUsername()) {
            throw new HxException(ResultEnum.UNKONW_ERROR);
        }
        return ordersService.givePayOrder(reOrders);
    }


    /**
     * 获取历史订单
     *
     * @param reOrder
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/hxminer/give/history/ordersDetails")
    public Result<Page<OrderDetails>> giveByHistoryOrderDetails(@Valid ReOrderNo reOrder) throws Exception {

        return ordersService.giveByHistoryOrderDetails(reOrder);

    }
}
